﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.ZhiJian
{
    public partial class ChanPin_Out : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int BillID = 0;
            if (!this.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out BillID);
                }
                if (BillID > 0)
                {
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    ////////////////////////    0       1           2           3          4       5          6       7        8          9       10          11      12    13      14          15
                    Cmd.CommandText = "select [编号],[产品类别ID],[合同ID],[订货单位ID],[炉号],[生产单位ID],[日期],[总件数],[总支数],[总重量],[总长度],[仓储主管],[库管员],[备注],[发货方式],[车号] from [产品出库单] where [ID]=" + BillID;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        this.ChanPin.DataBind();
                        this.ShengChan.DataBind();
                        this.CangChuZhuGuan.DataBind();
                        this.KuGuanYuan.DataBind();
                        this.FaHuoFangShi.DataBind();
                        this.CheHao.DataBind();

                        this.ViewState["BillID"] = BillID;
                        this.DanHao.Text = Reader.GetString(0).Trim();
                        this.DanHao.ReadOnly = true;
                        this.LuHao.Text = Reader.GetString(4);
                        this.ZongJian.Text = Reader.GetInt32(7).ToString();
                        this.ZongZhiShu.Text = Reader.GetInt32(8).ToString();

                        this.ZongZhong.Text = Reader.GetDouble(9).ToString();
                        this.ZongChang.Text = Reader.GetDouble(10).ToString();

                        this.RiQi.Text = Reader.GetDateTime(6).ToString("yyyy年M月d日");
                        this.BeiZhu.Text = Reader.GetString(13).Trim();

                        string temp = Reader.GetString(14);
                        if (this.FaHuoFangShi.Items.FindByValue(temp) != null)
                        {
                            this.FaHuoFangShi.Text = temp;
                        }
                        temp = Reader.GetInt32(5).ToString();
                        if (this.ShengChan.Items.FindByValue(temp) != null)
                        {
                            this.ShengChan.Text = temp;
                        }
                        temp = Reader.GetString(15);
                        if (this.CheHao.Items.FindByValue(temp) != null)
                        {
                            this.CheHao.Text = temp;
                        }

                        temp = Reader.GetInt32(1).ToString() + " " + (Reader.GetInt32(8) / Reader.GetInt32(7)).ToString(); ;
                        if (this.ChanPin.Items.FindByValue(temp) != null)
                        {
                            this.ChanPin.Text = temp;
                        }

                        temp = Reader.GetString(11).Trim();
                        if (this.CangChuZhuGuan.Items.FindByValue(temp) != null)
                        {
                            this.CangChuZhuGuan.Text = temp;
                        }
                        temp = Reader.GetString(12).Trim();
                        if (this.KuGuanYuan.Items.FindByValue(temp) != null)
                        {
                            this.KuGuanYuan.Text = temp;
                        }

                        Reader.Close();
                        Cmd.CommandText = "select [编号],[重量],[长度] from [产品库存] where [产品库存].[重量]<0 and [单据ID]=" + BillID;
                        Reader = Cmd.ExecuteReader();
                        for (int i = 1; i <= 28; i++)
                        {
                            if (!Reader.Read())
                            {
                           
                                break;
                            }
                            ((TextBox)this.FindControl("R" + i + "_C1")).Text = Reader.GetString(0);
                            ((TextBox)this.FindControl("R" + i + "_C2")).Text = (-Reader.GetFloat(1)).ToString();
                            ((TextBox)this.FindControl("R" + i + "_C3")).Text = (-Reader.GetFloat(2)).ToString();
                        }
                        Reader.Close();
                    }
                    else
                    {
                        Reader.Close();
                    }

                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                }
                else
                {
                    this.RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                }
            }









        }///////////


        protected void Button_Calc_Click(object sender, EventArgs e)
        {
            //int ZZhi = 0;//总支数
            float ZZhong = 0;//总重量
            int ZJian = 0;//总件数
            float ZChang = 0;//总长度
            this.Message.Text = "";

            //int n = int.Parse(this.ChanPin.Text.Split(" ".ToCharArray())[1]);
            float f1, f2;
            for (int i = 1; i <= 28; i++)
            {
                if (((TextBox)this.FindControl("R" + i + "_C1")).Text.Trim() != "")
                {
                    f1 = 0;
                    f2 = 0;
                    float.TryParse(((TextBox)this.FindControl("R" + i + "_C2")).Text, out f1);
                    float.TryParse(((TextBox)this.FindControl("R" + i + "_C3")).Text, out f2);
                    if (f1 == 0 || f2 == 0)
                    {
                        this.Message.Text += " ·捆 " + ((TextBox)this.FindControl("R" + i + "_C1")).Text + " 的捆重量或者捆长度输入有误";
                    }
                    else
                    {
                        //ZZhi += n;
                        ZZhong += f1;
                        ZChang += f2;
                        ZJian++;
                    }
                }
            }
            if (this.Message.Text == "")
            {
                //this.ZongZhiShu.Text = ZZhi.ToString();
                this.ZongZhong.Text = ZZhong.ToString();
                this.ZongJian.Text = ZJian.ToString();
                this.ZongChang.Text = ZChang.ToString();
                this.Message.Text = "·完成总数计算";
            }
        }

        protected void Button_Finish_Click(object sender, EventArgs e)
        {

            Button_Calc_Click(null, null);
            if (this.Message.Text != "·完成总数计算") return;

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlTransaction Tran = Conn.BeginTransaction();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.Transaction = Tran;
            try
            {
                Cmd.CommandText = "ChanPin_Out_Bill";
                Cmd.CommandType = CommandType.StoredProcedure;
                if (this.ViewState["BillID"] == null)
                {
                    this.ViewState["BillID"] = -1;
                }
                Cmd.Parameters.Add(new SqlParameter("@编号", SqlDbType.Char, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.DanHao.Text));
                Cmd.Parameters.Add(new SqlParameter("@产品类别ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(this.ChanPin.Text)));
                Cmd.Parameters.Add(new SqlParameter("@合同ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                Cmd.Parameters.Add(new SqlParameter("@订货单位ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                Cmd.Parameters.Add(new SqlParameter("@生产单位ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(this.ShengChan.Text)));
                Cmd.Parameters.Add(new SqlParameter("@炉号", SqlDbType.NChar, 40, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.LuHao.Text));
                Cmd.Parameters.Add(new SqlParameter("@日期", SqlDbType.Date, 3, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, DateTime.Parse(RiQi.Text)));

                Cmd.Parameters.Add(new SqlParameter("@总件数", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed,int.Parse(this.ZongJian.Text)));
                Cmd.Parameters.Add(new SqlParameter("@总支数", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed,int.Parse(this.ZongZhiShu.Text)));
                Cmd.Parameters.Add(new SqlParameter("@总重量", SqlDbType.Float, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed,float.Parse(this.ZongZhong.Text)));
                Cmd.Parameters.Add(new SqlParameter("@总长度", SqlDbType.Float, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, float.Parse(this.ZongChang.Text)));

                Cmd.Parameters.Add(new SqlParameter("@仓储主管", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.CangChuZhuGuan.Text));
                Cmd.Parameters.Add(new SqlParameter("@库管员", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.KuGuanYuan.Text));
                Cmd.Parameters.Add(new SqlParameter("@备注", SqlDbType.NVarChar, 400, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.BeiZhu.Text));
                Cmd.Parameters.Add(new SqlParameter("@发货方式", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.FaHuoFangShi.Text));
                Cmd.Parameters.Add(new SqlParameter("@车号", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.CheHao.Text));
                Cmd.Parameters.Add(new SqlParameter("@BillID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.ViewState["BillID"]));
                Cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));

                Cmd.ExecuteNonQuery();

                this.ViewState["BillID"] = (int)Cmd.Parameters["@ID"].Value;
                int BillID = (int)this.ViewState["BillID"];
                if (BillID > 0)
                {
                    Cmd.CommandText = "ChanPin_In";
                    Cmd.Parameters.Clear();
                    Cmd.Parameters.Add(new SqlParameter("@单据ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, BillID));
                    Cmd.Parameters.Add(new SqlParameter("@产品类别ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(this.ChanPin.Text)));
                    Cmd.Parameters.Add(new SqlParameter("@原料供货ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                    Cmd.Parameters.Add(new SqlParameter("@生产单位ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(this.ShengChan.Text)));
                    Cmd.Parameters.Add(new SqlParameter("@炉号", SqlDbType.NChar, 40, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.LuHao.Text));
                    Cmd.Parameters.Add(new SqlParameter("@编号", SqlDbType.NChar, 40, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, null));
                    Cmd.Parameters.Add(new SqlParameter("@日期", SqlDbType.Date, 3, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, DateTime.Parse(RiQi.Text)));
                    Cmd.Parameters.Add(new SqlParameter("@重量", SqlDbType.Real, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                    Cmd.Parameters.Add(new SqlParameter("@长度", SqlDbType.Real, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                    float f1, f2;
                    for (int i = 1; i <= 28; i++)
                    {

                        if (((TextBox)this.FindControl("R" + i + "_C1")).Text.Trim() != "")
                        {
                            f1 = 0;
                            f2 = 0;
                            float.TryParse(((TextBox)this.FindControl("R" + i + "_C2")).Text, out f1);
                            float.TryParse(((TextBox)this.FindControl("R" + i + "_C3")).Text, out f2);
                            if (f1 != 0 && f2 != 0)
                            {
                                Cmd.Parameters["@编号"].Value = ((TextBox)this.FindControl("R" + i + "_C1")).Text;
                                Cmd.Parameters["@重量"].Value = -f1;
                                Cmd.Parameters["@长度"].Value = -f2;
                                Cmd.ExecuteNonQuery();
                            }
                        }
                    }

                    this.Message.Text += "·保存成功";
                    this.DanHao.ReadOnly = true;
                }
                else
                {
                    this.Message.Text = "·单价编号(No.)已经存在";
                }
                Tran.Commit();
            }
            catch (Exception err)
            {
                Tran.Rollback();
                this.Message.Text = err.Message;
            }
            Cmd.Dispose();
            Tran.Dispose();
            Conn.Close();
            Conn.Dispose();
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            Response.Redirect("ChanPin_Out_Bill.aspx");
        }

        protected void ChanPin_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GuiGe.Text = this.ChanPin.SelectedItem.Text.Split(" ".ToCharArray())[1];
        }







        /////////////////////
    }
}
